<template>
  <div class="my">
    <!-- 头部 -->
    <header>
      <!-- 已登录 -->
      <div v-if="isLogin" class="login banner">
        <van-row></van-row>
        <van-row>
          <van-col
            span="11"
            offset="1"
            style="height: 100%; align-items: center"
          >
            <van-row type="flex" align="center">
              <van-image
                width="1.5rem"
                height="1.5rem"
                :src="userInfo.photo"
                round
              />
              <span class="mobileNum">{{ userInfo.name }}</span>
            </van-row>
          </van-col>
          <van-col
            span="6"
            offset="6"
            style="height: 100%; display: flex; align-items: center"
          >
            <van-button
              type="default"
              size="mini"
              round
              style="hight: 0.42667rem"
              @click="$router.push('/user')"
              >编辑资料</van-button
            >
          </van-col>
        </van-row>
        <van-row>
          <van-grid class="grid" :border="false">
            <van-grid-item text="头条"
              ><template #icon> {{ userInfo.art_count }}</template>
            </van-grid-item>

            <van-grid-item text="粉丝">
              <template #icon> {{ userInfo.fans_count }}</template>
            </van-grid-item>

            <van-grid-item text="关注"
              ><template #icon> {{ userInfo.follow_count }}</template>
            </van-grid-item>

            <van-grid-item text="获赞"
              ><template #icon> {{ userInfo.like_count }}</template>
            </van-grid-item>
          </van-grid>
        </van-row>
      </div>

      <!-- 未登录 -->
      <div v-else class="loginout banner">
        <div class="img-rap" @click="$router.push('/login')">
          <van-image width="1.76rem" height="1.76rem" :src="imgSrc"></van-image>
          <span class="text">登录 / 退出</span>
        </div>
      </div>
    </header>

    <!-- 主体 -->
    <main>
      <!-- 图标 -->
      <div>
        <van-grid class="grid-2" :column-num="2" clickable>
          <van-grid-item text="收藏">
            <template #icon>
              <span class="toutiao toutiao-shoucang"></span>
            </template>
          </van-grid-item>
          <van-grid-item text="历史">
            <template #icon>
              <span class="toutiao toutiao-lishi"></span>
            </template>
          </van-grid-item>
        </van-grid>
      </div>

      <!-- 链接 -->
      <div class="link">
        <van-cell title="消息通知" is-link />
        <van-cell title="小智同学" is-link />
      </div>
    </main>

    <!-- 底部 -->
    <footer>
      <van-button block class="exit" v-if="isLogin" @click="Logout">
        退出
      </van-button>
    </footer>
  </div>
</template>

<script>
// 引入vuex
import { mapGetters } from 'vuex'
import imgSrc from '@/assets/images/mobile.png'
import { getUserInfoAPI } from '@/api'
export default {
  data() {
    return {
      imgSrc,
      userInfo: {}
    }
  },

  // 在created生命周期里面发起请求
  created() {
    this.getUserInfo()
  },

  computed: {
    ...mapGetters(['isLogin'])
  },

  methods: {
    // .then方法就说明返回的是一个promise对象，代码冗余，直接用async和await拿到结果
    async Logout() {
      try {
        await this.$dialog.confirm({
          title: '黑马头条',
          message: '是否退出改账号？'
        })
        this.$store.commit('SET_TOKEN', {})
      } catch {}
    },
    // 分装请求函数
    async getUserInfo() {
      // 只有在登录的情况下才发起请求
      if (this.isLogin) {
        const { data } = await getUserInfoAPI()
        this.userInfo = data.data
      }
    }
  }
}
</script>

<style scoped lang="less">
.banner {
  width: 100%;
  height: 5.3333rem;
  background: url('@/assets/images/banner.png') no-repeat 0 0 / cover;
}

.loginout {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .img-rap {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .text {
    color: #fff;
    font-size: 0.4rem;
    margin-top: 0.26667rem;
  }
}

// 已登录
.login {
  display: flex;
  flex-direction: column;
  // 防止后面的嵌套耦合，用子代选择器
  > .van-row {
    flex: 1;
    .mobileNum {
      color: #fff;
      font-size: 0.4rem;
      margin-left: 0.4rem;
    }
  }
}
:deep(.grid) {
  color: #fff;
  .van-grid-item__content {
    background-color: unset;
  }

  .van-grid-item__text {
    color: #fff;
  }
  .van-grid-item__icon-wrapper {
    font-size: 0.4rem;
  }
}
.grid-2 {
  .toutiao {
    font-size: 40px;
  }
  .toutiao-shoucang {
    color: red;
  }
  .toutiao-lishi {
    color: orange;
  }
}
.my {
  height: calc(100vh - 100px);
  background-color: #f5f7f9;
}
.link {
  margin: 10px 0;
}
.exit {
  .van-button__content {
    color: red;
  }
}
</style>
